<template>
  <div>
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '修改用户'"
      :visible.sync="isShow"
    >
      <commenform
        :formlabel="operateFormlabel"
        :form="operateForm"
        :inline="true"
        ref="form"
      ></commenform>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirm">确定</el-button>
        <el-button @click="isShow = false">取消</el-button>
      </div>
    </el-dialog>
    <div class="manage-hearder">
      <el-button type="primary" @click="addUser">添加用户</el-button>
      <commenform
        :formlabel="Formlabel"
        :form="searchoperateForm"
        :inline="true"
        ref="form"
      >
        <el-button type="primary" @click="getList">搜索</el-button>
      </commenform>
    </div>
    <div>
      <commentable
        :tableData="tableData"
        :tableLable="tableLable"
        :config="config"
        @changePage="getList()"
        @edit="enitUser"
        @del="delUser"
      ></commentable>
    </div>
  </div>
</template>
<script>
import commenform from "../../components/commenform.vue";
import commentable from "../../components/commentable.vue";
import { getUser } from "../../api/data";
export default {
  name: "User",
  components: {
    commenform,
    commentable,
  },
  data() {
    return {
      operateType: "add",
      isShow: false,
      operateFormlabel: [
        {
          model: "name",
          label: "姓名",
          type: "input",
        },
        {
          model: "age",
          label: "年龄",
          type: "input",
        },
        {
          model: "sex",
          label: "性别",
          type: "select",
          opts: [
            {
              label: "男",
              value: 1,
            },
            {
              label: "女",
              value: 0,
            },
          ],
        },
        {
          model: "birth",
          label: "出生日期",
          type: "date",
        },
        {
          model: "addr",
          label: "地址",
          type: "input",
        },
      ],
      operateForm: {
        name: "",
        addr: "",
        age: "",
        brith: "",
        sex: "",
      },
      Formlabel: [
        {
          model: "keyword",
          label: "",
          type: "input",
        },
      ],
      searchoperateForm: {
        keyword: "",
      },
      tableData: [],
      tableLable: [
        {
          prop: "name",
          label: "姓名",
        },
        {
          prop: "age",
          label: "年龄",
        },
        {
          prop: "sex",
          label: "性别",
        },
        {
          prop: "birth",
          label: "出生时间",
          width: 200,
        },
        {
          prop: "addr",
          label: "地址",
          width: 320,
        },
      ],
      config: {
        page:1,
        total:10,
        loading:true
      },
    };
  },
  methods: {
    confirm() {
      if (this.operateType === "edit") {
        this.$http.post("/user/edit", this.operateForm).then((res) => {
          this.getList();
          this.isShow = false;
        });
      } else {
        this.$http.post("/user/add", this.operateForm).then((res) => {
          this.isShow = false;
        });
      }
    },
    getList(name = '') {
      this.config.loading = true;
      name ? (this.config.page = 1) : '';
      getUser({
        
        page: this.config.page,
        name,
        
      }).then(({ data: res }) => {
        console.log(res, "----------------");
        this.tableData = res.list.map((item) => {
          item.sexLable = item.sex === 0 ? "女" : "男";

          return item;
        });
        console.log(this.tableData);
        this.config.total = res.count;
        this.config.loading = false;
      });
    },
    addUser() {
      this.isShow = true;
      this.operateType = "add";
      this.operateForm = {
        name: "",
        addr: "",
        age: "",
        brith: "",
        sex: "",
      };
    },
    enitUser(row) {
      this.operateType = "edit";
      this.isShow = true;
      this.operateForm = row;
    },
    delUser(row) {
      this.$confirm("此操作将永久删除", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const id = row.id;
        this.$http
          .get("user/del", {
            params: { id },
          })
          .then(() => {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.getList();
          });
      });
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.manage-hearder {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>